package com.tcfvic.client.panels.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.TextBox;
import com.tcfvic.client.PersonService;
import com.tcfvic.client.PersonServiceAsync;
import com.tcfvic.client.panels.widgets.addons.advancedtable.AdvancedTable;
import com.tcfvic.client.panels.widgets.addons.advancedtable.DataFilter;
import com.tcfvic.client.panels.widgets.addons.advancedtable.RowSelectionListener;
import com.tcfvic.client.panels.widgets.addons.advancedtable.TableModelServiceAsync;

public class FundingSourceDetailsListWidget extends FlexTable implements
		RowSelectionListener {
	private final PersonServiceAsync personService = GWT
			.create(PersonService.class);
	private TextBox sourceNameTextBox = new TextBox();
	private AdvancedTable at = new AdvancedTable();

	public FundingSourceDetailsListWidget(
			TableModelServiceAsync tableModelServiceAsync,
			final FundingSourceDetailsInputWidget rowSelectionListener) {
		super();
		at = new AdvancedTable();

		// Create a table to layout the form options
		setCellSpacing(4);
		setWidth("100%");
		FlexCellFormatter cellFormatter = getFlexCellFormatter();

		// Add a title to the form
		at.setFirstColumnVisible(false);
		at.setPageSize(4);
		// at.setHeight("300 px");
		at.setWidth("100%");
		at.addRowSelectionListener(rowSelectionListener);

		// last action to be performed
		at.setTableModelService(tableModelServiceAsync);

		sourceNameTextBox.addKeyUpHandler(new KeyUpHandler() {
			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getSource() instanceof TextBox) {
					TextBox filter = (TextBox) event.getSource();
					DataFilter[] df = new DataFilter[2];
					df[0] = new DataFilter("sourceName", filter.getText());
					at.applyFilters(df);
				}
			}
		});

		// Add some standard form options
		// create new record button
		Button newButton = new Button("Create new");
		newButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				rowSelectionListener.createNewRecord();
			}
		});
		setWidget(0, 0, newButton);

		final Button deleteButton = new Button("");
		personService.isAdminUser(new AsyncCallback<Boolean>() {
			@Override
			public void onSuccess(Boolean result) {
				if (result) {
					deleteButton.setText("Delete");
					deleteButton.addClickHandler(new ClickHandler() {
						@Override
						public void onClick(ClickEvent event) {
							personService.deleteFundingSource(Integer
									.parseInt(at.getSelectedRowId()),
									new AsyncCallback<Void>() {
										@Override
										public void onFailure(Throwable caught) {
											new SimpleMessagePopup(
													"The request to delete the funding source failed.",
													null).showPopup();
										}

										@Override
										public void onSuccess(Void result) {
											at.updateTableData();
										}
									});
						}
					});
				} else {
					deleteButton.setVisible(false);
				}

			}

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub

			}
		});
		setWidget(0, 1, deleteButton);
		setHTML(1, 0, "Source name");
		setWidget(1, 1, sourceNameTextBox);
		setWidget(2, 0, at);
		cellFormatter.setColSpan(2, 0, 4);

	}

	public void updateTableData() {
		at.updateTableData();
	}

	@Override
	public void onRowSelected(AdvancedTable sender, String rowId) {
		// TODO Auto-generated method stub

	}
}
